package com.cn.xiaonuo.sys.modular.images;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.xiaonuo.core.context.login.LoginContextHolder;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.login.SysLoginUser;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.sys.config.RedisService;
import com.cn.xiaonuo.sys.modular.images.mapper.ImagesMapper;
import com.cn.xiaonuo.sys.modular.images.vo.ImagesVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/wiki/")
public class WikipediaController {

    @Autowired
    private ImagesMapper imagesMapper;

    @Autowired
    private RedisService redisService;


    @GetMapping("/index")
    public String list(){
        return "business/wiki/index.html";
    }

    @GetMapping("form")
    public String form(){
        log.info("跳转表单提交");
        return "business/wiki/form.html";
    }

    /**
     * 查看详情
     * @return
     */
    @GetMapping("/detailPage")
    public String detailPage() {
        return "business/wiki/detail.html";
    }


    /**
     * 分页查询
     * @param imagesVo
     * @return
     */
    @GetMapping("page")
    @ResponseBody
    public PageResult<Images> finImagesPage(ImagesVo imagesVo){
        QueryWrapper<Images> queryWrapper=new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(imagesVo)){
            if (!StringUtils.isEmpty(imagesVo.getName())){
                queryWrapper.like("name",imagesVo.getName());
            }
        }
        queryWrapper.eq("type", Images.type.WIKI);
        queryWrapper.orderByAsc("num");
        Page<Images> ImagesPage = imagesMapper.selectPage(PageFactory.defaultPage(), queryWrapper);
        return new PageResult<>(ImagesPage);
    }

    /**
     * 新增轮播图
     * @param images
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResponseData addimg(@RequestBody Images images){
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        images.setUserid(sysLoginUser.getId());
        images.setUsername(sysLoginUser.getUsername());
        images.setCreateTime(new Date());
        images.setType(Images.type.WIKI);
        imagesMapper.insert(images);
        redisService.del("NEWS_IMAGES_WIKI");
        return new SuccessResponseData();
    }

    /**
     * 编辑轮播图表单提交
     * @param images
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public ResponseData editImg(@RequestBody Images images){
        images.setUpdateTime(new Date());
        images.setType(Images.type.WIKI);
        imagesMapper.updateById(images);
        redisService.del("NEWS_IMAGES_WIKI");
        return new SuccessResponseData();
    }

    /**
     * 删除轮播图
     * @param images
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResponseData deleteImg(@RequestBody List<Images> images){
        for (Images image : images) {
            imagesMapper.deleteById(image.getId());
        }
        redisService.del("NEWS_IMAGES_WIKI");
        return new SuccessResponseData();
    }

    /**
     * 查询轮播图详情
     * @param images
     * @return
     */
    @GetMapping("detail")
    @ResponseBody
    public ResponseData detail(Images images){
        Images images1 = imagesMapper.selectById(images.getId());
        return new SuccessResponseData(images1);
    }

}
